<template>
  <div class="login-page">
    <el-card class="box-card">
      <template #header>静静小店运营平台</template>
      <div>
        <el-form :model="ruleForm" :rules="loginRules" ref="loginRef">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username" palceholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" palceholder="请输入密码"  show-password ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
            <el-button>重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { login } from "@/api/user"
import { setToken } from "@/utils/storage"
export default {
  name: 'Login',
  data() {
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      loginRules: {
        //要校验的字段:[{规则},....]
        username: [
          // 必填校验
          {
            required: true,
            message: "用户名必填",
            trigger: ["blur", "change"]
          },
          //格式校验
          {
            pattern: /^\w{5,17}$/,
            message: "用户名必须是5-17位字符",
            trigger: ["blur", "change"]
          }
        ],
        password: [
          {
            required: true,
            message: "密码必填",
            trigger: ["blur", "change"]
          },
          //格式校验
          {
            pattern: /^\w{5,11}$/,
            message: "用户名必须是5-11位密码",
            trigger: ["blur", "change"]
          }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.loginRef.validate(async(valid) => {
        // console.log(2222);
        if(valid){
          const res = await login (this.ruleForm)
          console.log(res);
          // token 存全局
          this.$store.commit("user/upToken",res.data.token)
          // token 存本地
          setToken(res.data.token)
          // 提示
          this.$message({
          message: '登录成功',
          type: 'success'
        });
        // 跳转
        this.$router.push('/')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  // 利用 flex 控制水平垂直居中
  display: flex;
  align-items: center;
  justify-content: space-around;

  .tc {
    text-align: center;
  }

  .el-card {
    width: 420px;

    //样式穿透
    // css  父级>>>子集
    // less 父级 /deep/ 子集
    // scss 父级 ::v-deep 子集 
    ::v-deep .el-card__header {
      background-color: rgba(114, 124, 245, 1);
      color: #fff;
      font-size: 18px;
      text-align: center;
      height: 80px;
      line-height: 44px;
    }

    ::v-deep .el-form-item__content {
      text-align: center;
    }
  }
}
</style>@/api/user